// packageE/pages/home/home.js
const mybehavior = require("../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    active: "home",
    area: "",
    area_list: [],
    area_list1: [],
    show: false,
    search_value: "",
  },


  onSearch1(e) {
    if (e.detail == "") {
      let arr = this.data.area_list1
      this.setData({
        area_list: arr
      })
    } else {

      let list1 = this.data.area_list1
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        area_list: arr
      })

    }
  },
  // 切换园区
  changeArea() {
    this.setData({
      show: true
    })

  },
  cancelArea() {
    this.setData({
      show: false
    })
  },
  onChangeArea(e) {
    let val = e.detail
    this.setData({
      area: val
    })
    wx.setStorageSync('area', val);
  },
  onCellChange(e) {
    let val = e.currentTarget.dataset.row
    this.setData({
      area: val
    })
    wx.setStorageSync('area', val);
  },
  // 查询园区
  getArea() {
    let url = "https://logisticsengineering.gree.com/vehicle_reservation_api/campus_supplier_manage/get_area";
    let data = {
      // _id: wx.getStorageSync('_id')
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          area_list: res.data,
          area_list1: res.data,
        })
      } else {
        this.setData({
          area_list: [],
          area_list1: [],
        })
        // wx.showToast({
        //   title: res.msg,
        //   icon: "error"
        // })
      }
    }).catch(err => {
      this.setData({
        area_list1: [],
        area_list: []
      })
      // wx.showToast({
      //   title: err.msg,
      //   icon: 'error' 
      // })
    })
  },
  routeJump(e) {
    wx.requestSubscribeMessage({
      tmplIds: ['Qq4EMCt24ZVzeR74sHCcdCgZyo7pus8Bf9knNoUHPpM'],
    })
    let key = e.currentTarget.dataset.row_data
    wx.navigateTo({
      url: key,
    })
  },
  onChange(event) {
    // console.log(event.detail);
    this.setData({ active: event.detail });
    let val = event.detail
    if (val == "home") {
      wx.redirectTo({
        url: '/pages/home2/home2',
      })
    } else if (val == "search") {
      wx.redirectTo({
        url: '/packageC/pages/package/workbench/workbench',
      })
    } else {
      wx.redirectTo({
        url: '/packageE/pages/personal_center/personal_center',
      })
    }
  },
  // 判断是否为供应商
  judgeAdmin() {
    let url = "";
    const accountInfo = wx.getAccountInfoSync();
    // console.log(accountInfo);
    if (accountInfo.miniProgram.envVersion === 'release') {

      url = "https://logisticsengineering.gree.com/vehicle_reservation_api/entrance_manage/judge_admin"
    } else {

      url = "https://logisticsengineering.gree.com/vehicle_reservation_api/entrance_manage/judge_admin"
    }

    let data = {}
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        if (res.data == "是") {
          wx.setStorageSync('gys_show', true)

        } else {
          wx.setStorageSync('gys_show', false)
        }
      } else {
        // wx.showToast({
        //   title: res.msg,
        //   icon: "error"
        // })
      }
    }).catch(err => {
      // wx.showToast({
      //   title: err.msg,
      //   icon: 'none'
      // })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.judgeAdmin()
    this.getArea()
    let supplier_number_arr = wx.getStorageSync('department').split('_')
    let area = wx.getStorageSync('area')
    this.setData({
      area: area
    })
    if (supplier_number_arr.length >= 2) {
      wx.setStorageSync('supplier_number', supplier_number_arr[0]);
      wx.setStorageSync('unit', supplier_number_arr[1]);
    } else {
      wx.setStorageSync('supplier_number', "");
      wx.setStorageSync('unit', "");
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})